package com.unittec.drainage.provider.auth.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.unittec.drainage.provider.auth.constant.AuthConstant;
import com.unittec.drainage.provider.auth.entity.*;
import com.unittec.drainage.provider.auth.service.*;
import com.unittec.drainage.provider.utils.*;
import com.unittec.drainage.sdk.auth.domain.SysUserInfo;
import com.unittec.drainage.sdk.auth.request.*;
import com.unittec.drainage.sdk.auth.response.*;
import com.unittec.drainage.provider.auth.entity.*;
import com.unittec.drainage.provider.auth.service.*;
import com.unittec.drainage.provider.log.Log;
import com.unittec.drainage.provider.scope.ThreadLocalScope;
import com.unittec.drainage.sdk.auth.domain.SysUserRes;
import com.unittec.drainage.sdk.auth.enums.BizErrorCode101;
import com.unittec.drainage.sdk.log.enums.LogOperateEnum;
import com.unittec.drainage.sdk.log.enums.LogStatusEnum;
import com.unittec.drainage.sdk.root.common.CommonConstant;
import com.unittec.drainage.sdk.root.domain.GatewayRequest;
import com.unittec.drainage.sdk.root.domain.Result;
import com.unittec.drainage.sdk.root.enums.BizCode;
import com.unittec.drainage.sdk.root.enums.BizCodeEnum;
import com.unittec.drainage.sdk.root.internal.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author chengjie
 * @description 用户管理
 * @date 2019/10/23
 */
@RestController
@Api(tags = "用户管理")
public class SysUserController {

    @Autowired
    Log log;
    @Value("${rsa.privateKey}")
    private String privateKey;
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysDepartService sysDepartService;

    @Autowired
    private SysUserDepartService sysUserDepartService;

    @RequestMapping(value = "/101100", method = RequestMethod.POST)
    @ApiOperation(value = "查询所有用户列表", notes = "查询所有用户列表")
    @ResponseBody
    public Result<Res101100> selectAllUserList(@RequestBody GatewayRequest<Req101100> req) {
        log.debug(req, "selectAllUserList {}  ", "begin");
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101100 res101100 = null;
        try {
            Map<String,Object> paramMap =new HashMap<>();
            if(StringUtils.isNotBlank(req.getContent().getRealName())){
                paramMap.put("realName",req.getContent().getRealName());
            }
            if(StringUtils.isNotBlank(req.getContent().getPlatformCode())){
                paramMap.put("platformCode",req.getContent().getPlatformCode());
            }
            List<Map<String, Object>> list = sysUserService.selectAllUserList(paramMap);
            res101100 = new Res101100();
            res101100.setList(list);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "selectAllUserList {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101100);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101101", method = RequestMethod.POST)
    @ApiOperation(value = "查询用户分页列表", notes = "查询用户分页列表")
    @ResponseBody
    public Result<Res101101> getUserPage(@RequestBody GatewayRequest<Req101101> req) {
        log.debug(req, "getUserPage {}  ", "begin");
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101101 res101101 = null;
        try {
            Page<Map<String, Object>> pageList = sysUserService.selectSysUserPage(req.getContent());
            res101101 = new Res101101();
            res101101.setPage(pageList);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getUserPage {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101101);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101102", method = RequestMethod.POST)
    @ApiOperation(value = "添加用户", notes = "添加用户")
    @ResponseBody
    public Result<Res101102> addUser(@RequestBody GatewayRequest<Req101102> req) {
        log.debug(req, "addUser {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101102 res101102 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            String platformCode = req.getContent().getPlatformCode();
            if (StringUtils.isNotBlank(platformCode)) {
                if (platformCode.indexOf(AuthConstant.PLATFORM_CODE_222222) != -1) {
                    String type = req.getContent().getType();
                    if (StringUtils.isBlank(type)) {
                        throw new BusinessException("用户类型不能为空");
                    }
                } else {
                    req.getContent().setType("");
                }
            }
            SysUser sysUser = new SysUser();
            BeanUtils.copyProperties(req.getContent(), sysUser);
            String roleIds = req.getContent().getRoleIds();
            String departIds = req.getContent().getDepartIds();
            String salt = GenerateCodeUtil.randomGen(8);
            sysUser.setSalt(salt);
            String passwordEncode = PasswordUtil.encrypt(sysUser.getUsername(), sysUser.getPassword(), salt);
            sysUser.setPassword(passwordEncode);
            sysUser.setStatus(CommonConstant.STATUS_0);
            sysUser.setDelFlag(CommonConstant.DELETE_NO);
            sysUserService.addUser(sysUser, roleIds, departIds);
            res101102 = new Res101102();
            res101102.setIsSuccess(true);
            res101102.setId(sysUser.getId());
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("101102", JSON.toJSONString(req), "添加用户", log.buildErrmsg(e),
                LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "addUser {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("101102", JSON.toJSONString(req), "添加用户", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res101102);
        } else {
            log.addOperateLog("101102", JSON.toJSONString(req), "添加用户", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101103", method = RequestMethod.POST)
    @ApiOperation(value = "修改用户", notes = "修改用户")
    @ResponseBody
    public Result<Res101103> editUser(@RequestBody GatewayRequest<Req101103> req) {
        log.debug(req, "editUser {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101103 res101103 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            String platformCode = req.getContent().getPlatformCode();
            if (StringUtils.isNotBlank(platformCode)) {
                if (platformCode.indexOf(AuthConstant.PLATFORM_CODE_222222) != -1) {
                    String type = req.getContent().getType();
                    if (StringUtils.isBlank(type)) {
                        throw new BusinessException("用户类型不能为空");
                    }
                } else {
                    req.getContent().setType("");
                }
            }
            SysUser sysUser = sysUserService.getById(req.getContent().getId());
            if (sysUser != null) {
                BeanUtils.copyProperties(req.getContent(), sysUser);
                String roleIds = req.getContent().getRoleIds();
                String departIds = req.getContent().getDepartIds();
                sysUserService.editUser(sysUser, roleIds, departIds);
                res101103 = new Res101103();
                res101103.setIsSuccess(true);
                bizCodeEnum = BizCode.SUCCESS;
            } else {
                bizCodeEnum = BizCode.NOT_USER;
            }
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("101103", JSON.toJSONString(req), "修改用户", log.buildErrmsg(e),
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "editUser {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("101103", JSON.toJSONString(req), "修改用户", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res101103);
        } else {
            log.addOperateLog("101103", JSON.toJSONString(req), "修改用户", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101104", method = RequestMethod.POST)
    @ApiOperation(value = "冻结/解冻用户", notes = "冻结/解冻用户")
    @ResponseBody
    public Result<Res101104> frozenBatch(@RequestBody GatewayRequest<Req101104> req) {
        log.debug(req, "frozenBatch {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101104 res101104 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            String ids = req.getContent().getIds();
            String status = req.getContent().getStatus();
            String[] array = ids.split(",");
            for (String id : array) {
                if (id != null) {
                    sysUserService.update(new SysUser().setStatus(status),
                        new UpdateWrapper<SysUser>().lambda().eq(SysUser::getId, id));
                }
            }
            res101104 = new Res101104();
            res101104.setIsSuccess(true);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("101104", JSON.toJSONString(req), "冻结/解冻用户", log.buildErrmsg(e),
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "frozenBatch {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("101104", JSON.toJSONString(req), "冻结/解冻用户", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res101104);
        } else {
            log.addOperateLog("101104", JSON.toJSONString(req), "冻结/解冻用户", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101105", method = RequestMethod.POST)
    @ApiOperation(value = "批量删除用户", notes = "批量删除用户")
    @ResponseBody
    public Result<Res101105> deleteBatch(@RequestBody GatewayRequest<Req101105> req) {
        log.debug(req, "deleteBatch {}  ", "end");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101105 res101105 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            sysUserService.deleteBatch(req.getContent().getIds());
            res101105 = new Res101105();
            res101105.setIsSuccess(true);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("101105", JSON.toJSONString(req), "批量删除用户", log.buildErrmsg(e),
                LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "deleteBatch {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("101104", JSON.toJSONString(req), "批量删除用户", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res101105);
        } else {
            log.addOperateLog("101104", JSON.toJSONString(req), "批量删除用户", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101106", method = RequestMethod.POST)
    @ApiOperation(value = "用户详情", notes = "用户详情")
    @ResponseBody
    public Result<Res101106> getUserDetails(@RequestBody GatewayRequest<Req101106> req) {
        log.debug(req, "getUserDetails {}  ", "begin");
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101106 res101106 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            SysUser sysUser = sysUserService.getById(req.getContent().getId());
            if (sysUser != null) {
                res101106 = new Res101106();
                BeanUtils.copyProperties(sysUser, res101106);

                if(StringUtils.isNotBlank(res101106.getAvatar())){
                   String url= OssUtils.getUrl(res101106.getAvatar(),1);
                    res101106.setAvatarUrl(url);
                }

                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(req.getContent().getId());
                QueryWrapper<SysUserRole> sysUserRoleQueryWrapper = new QueryWrapper<SysUserRole>();
                sysUserRoleQueryWrapper.setEntity(sysUserRole);
                List<SysUserRole> sysUserRoleList = sysUserRoleService.list(sysUserRoleQueryWrapper);
                if (!CollectionUtils.isEmpty(sysUserRoleList)) {
                    List<String> roleIds = new ArrayList<String>();
                    sysUserRoleList.forEach((n -> roleIds.add(n.getRoleId())));
                    res101106.setRoleIds(roleIds);
                }
                SysUserDepart sysUserDepart = new SysUserDepart();
                sysUserDepart.setUserId(req.getContent().getId());
                QueryWrapper<SysUserDepart> sysUserDepartQueryWrapper = new QueryWrapper<SysUserDepart>();
                sysUserDepartQueryWrapper.setEntity(sysUserDepart);
                List<SysUserDepart> sysUserDepartList = sysUserDepartService.list(sysUserDepartQueryWrapper);
                if (!CollectionUtils.isEmpty(sysUserDepartList)) {
                    List<String> departIds = new ArrayList<String>();
                    List<String> departNames = new ArrayList<String>();
                    List<String> departLevels = new ArrayList<String>();
                    for (SysUserDepart depart : sysUserDepartList) {
                        departIds.add(depart.getDepId());
                        SysDepart sysDepart = sysDepartService.getById(depart.getDepId());
                        if (sysDepart != null) {
                            departNames.add(sysDepart.getDepartName());
                            departLevels.add(String.valueOf(sysDepart.getDepartLevel()));
                        }
                    }

                    res101106.setDepartIds(departIds);
                    if (departNames.size() > 0) {
                        res101106.setDepartName(String.join(",", departNames));
                    }
                    if (departLevels.size() > 0) {
                        res101106.setDepartLevel(String.join(",", departLevels));
                    }
                }
                bizCodeEnum = BizCode.SUCCESS;
            } else {
                bizCodeEnum = BizCode.NOT_USER;
            }
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getUserDetails {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101106);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101107", method = RequestMethod.POST)
    @ApiOperation(value = "修改用户密码", notes = "修改用户密码")
    @ResponseBody
    public Result<Res101107> changPassword(@RequestBody GatewayRequest<Req101107> req) {
        log.debug(req, "changPassword {}  ", "end");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101107 res101107 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            String id = req.getContent().getId();
            String password = req.getContent().getPassword();

            if(StringUtils.isBlank(password)){
                password= PasswordRandomUtil.randomPassword();
            }

            SysUser sysUser = sysUserService.getById(id);
            if (sysUser != null) {
                String salt = GenerateCodeUtil.randomGen(8);
                sysUser.setSalt(salt);
                String passwordEncode = PasswordUtil.encrypt(sysUser.getUsername(), password, salt);
                sysUser.setPassword(passwordEncode);
                sysUserService.updateById(sysUser);
                bizCodeEnum = BizCode.SUCCESS;
                res101107 = new Res101107();
                res101107.setIsSuccess(true);
                res101107.setPassword(password);
            } else {
                bizCodeEnum = BizCode.NOT_USER;
            }
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("101107", JSON.toJSONString(req), "修改用户密码", log.buildErrmsg(e),
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "changPassword {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("101107", JSON.toJSONString(req), "修改用户密码", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res101107);
        } else {
            log.addOperateLog("101107", JSON.toJSONString(req), "修改用户密码", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101108", method = RequestMethod.POST)
    @ApiOperation(value = "根据idList查询用户列表", notes = "根据idList查询用户列表")
    @ResponseBody
    public Result<Res101108> getSysUserListByIdList(@RequestBody GatewayRequest<Req101108> req) {
        log.debug(req, "getSysUserListByIdList {}  ", "end");
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101108 res101108 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            List<SysUser> list = sysUserService.getSysUserListByIdList(req.getContent().getIdList());
            bizCodeEnum = BizCode.SUCCESS;
            res101108 = new Res101108();
            res101108.setList(CopyListUtils.copyList(list, SysUserRes.class));
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getSysUserListByIdList {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101108);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101109", method = RequestMethod.POST)
    @ApiOperation(value = "根据用户id查询用户所有角色id列表", notes = "根据用户id查询用户所有角色id列表")
    @ResponseBody
    public Result<Res101109> queryUserRole(@RequestBody GatewayRequest<Req101109> req) {
        log.debug(req, "queryUserRole {}  ", "end");
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101109 res101109 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            List<SysUserRole> sysUserRoleList = sysUserRoleService
                .list(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getUserId, req.getContent().getId()));
            res101109 = new Res101109();
            if (CollectionUtils.isEmpty(sysUserRoleList)) {
                res101109.setList(new ArrayList<>());
            } else {
                res101109.setList(
                    sysUserRoleList.stream().map(sysUserRole -> sysUserRole.getRoleId()).collect(Collectors.toList()));
            }
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "queryUserRole {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101109);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101110", method = RequestMethod.POST)
    @ApiOperation(value = "校验用户名是否唯一", notes = "校验用户名是否唯一")
    @ResponseBody
    public Result<Res101110> checkOnlyUsername(@RequestBody GatewayRequest<Req101110> req) {
        log.debug(req, "checkOnlyUsername {}  ", "end");
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101110 res101110 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            SysUser sysUser = new SysUser();
            sysUser.setUsername(req.getContent().getUsername());
            sysUser = sysUserService.getOne(new QueryWrapper<SysUser>(sysUser));
            res101110 = new Res101110();
            res101110.setIsSuccess(sysUser == null);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "checkOnlyUsername {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101110);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101111", method = RequestMethod.POST)
    @ApiOperation(value = "根据部门id查询用户列表", notes = "根据部门id查询用户列表")
    @ResponseBody
    public Result<Res101111> queryUserListByDepId(@RequestBody GatewayRequest<Req101111> req) {
        log.debug(req, "queryUserListByDepId {}  ", "end");
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101111 res101111 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            List<SysUser> sysUserList = sysUserDepartService.queryUserByDepId(req.getContent().getDepId());
            res101111 = new Res101111();
            List<SysUserRes> list = CopyListUtils.copyList(sysUserList, SysUserRes.class);
            res101111.setList(list);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "queryUserListByDepId {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101111);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101112", method = RequestMethod.POST)
    @ApiOperation(value = "给指定角色添加用户", notes = "给指定角色添加用户")
    @ResponseBody
    public Result<Res101112> addSysUserRole(@RequestBody GatewayRequest<Req101112> req) {
        log.debug(req, "addSysUserRole {}  ", "end");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101112 res101112 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            String roleId = req.getContent().getRoleId();
            for (String userId : req.getContent().getUserIdList()) {
                if (StringUtils.isNotBlank(userId)) {
                    SysUserRole sysUserRole = new SysUserRole(userId, roleId);
                    QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<SysUserRole>();
                    queryWrapper.eq("role_id", roleId).eq("user_id", userId);
                    SysUserRole one = sysUserRoleService.getOne(queryWrapper);
                    if (one == null) {
                        sysUserRoleService.save(sysUserRole);
                    }
                }
            }
            res101112 = new Res101112();
            res101112.setIsSuccess(true);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("101112", JSON.toJSONString(req), "给指定角色添加用户", log.buildErrmsg(e),
                LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "addSysUserRole {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("101112", JSON.toJSONString(req), "给指定角色添加用户", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res101112);
        } else {
            log.addOperateLog("101112", JSON.toJSONString(req), "给指定角色添加用户", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101113", method = RequestMethod.POST)
    @ApiOperation(value = "删除指定角色的用户关系", notes = "删除指定角色的用户关系")
    @ResponseBody
    public Result<Res101113> deleteUserRole(@RequestBody GatewayRequest<Req101113> req) {
        log.debug(req, "deleteUserRole {}  ", "end");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101113 res101113 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<SysUserRole>();
            queryWrapper.eq("role_id", req.getContent().getRoleId()).eq("user_id", req.getContent().getUserId());
            sysUserRoleService.remove(queryWrapper);
            res101113 = new Res101113();
            res101113.setIsSuccess(true);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("101113", JSON.toJSONString(req), "删除指定角色的用户关系", log.buildErrmsg(e),
                LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "deleteUserRole {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("101113", JSON.toJSONString(req), "删除指定角色的用户关系", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res101113);
        } else {
            log.addOperateLog("101113", JSON.toJSONString(req), "删除指定角色的用户关系", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101114", method = RequestMethod.POST)
    @ApiOperation(value = "批量删除指定角色的用户关系", notes = "批量删除指定角色的用户关系")
    @ResponseBody
    public Result<Res101114> batchDeleteUserRole(@RequestBody GatewayRequest<Req101114> req) {
        log.debug(req, "batchDeleteUserRole {}  ", "end");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101114 res101114 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<SysUserRole>();
            queryWrapper.eq("role_id", req.getContent().getRoleId()).in("user_id",
                Arrays.asList(req.getContent().getUserIds().split(",")));
            sysUserRoleService.remove(queryWrapper);
            res101114 = new Res101114();
            res101114.setIsSuccess(true);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("101114", JSON.toJSONString(req), "批量删除指定角色的用户关系", log.buildErrmsg(e),
                LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "batchDeleteUserRole {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("101114", JSON.toJSONString(req), "批量删除指定角色的用户关系", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res101114);
        } else {
            log.addOperateLog("101114", JSON.toJSONString(req), "批量删除指定角色的用户关系", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101115", method = RequestMethod.POST)
    @ApiOperation(value = "获取部门用户分页列表", notes = "获取部门用户分页列表")
    @ResponseBody
    public Result<Res101115> getDepartUserPage(@RequestBody GatewayRequest<Req101115> req) {
        log.debug(req, "getDepartUserPage {}  ", "begin");
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101115 res101115 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Page<SysUser> page = new Page<SysUser>(req.getContent().getCurrent(), req.getContent().getSize());
            IPage<SysUser> pageList =
                sysUserService.getDepartUserPage(page, req.getContent().getDepId(), req.getContent().getUsername());
            res101115 = new Res101115();
            res101115.setPage(pageList);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getDepartUserPage {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101115);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101116", method = RequestMethod.POST)
    @ApiOperation(value = "获取角色用户分页列表", notes = "获取角色用户分页列表")
    @ResponseBody
    public Result<Res101116> getRoleUserPage(@RequestBody GatewayRequest<Req101116> req) {
        log.debug(req, "getRoleUserPage {}  ", "begin");
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101116 res101116 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Page<SysUser> page = new Page<SysUser>(req.getContent().getCurrent(), req.getContent().getSize());
            IPage<SysUser> pageList =
                sysUserService.getRoleUserPage(page, req.getContent().getRoleId(), req.getContent().getUsername());
            res101116 = new Res101116();
            res101116.setPage(pageList);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getRoleUserPage {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101116);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101117", method = RequestMethod.POST)
    @ApiOperation(value = "给指定部门添加用户", notes = "给指定部门添加用户")
    @ResponseBody
    public Result<Res101117> addSysUserDepart(@RequestBody GatewayRequest<Req101117> req) {
        log.debug(req, "addSysUserDepart {}  ", "end");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101117 res101117 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            String depId = req.getContent().getDepId();
            for (String userId : req.getContent().getUserIdList()) {
                if (StringUtils.isNotBlank(userId)) {
                    SysUserDepart sysUserDepart = new SysUserDepart(userId, depId);
                    QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<SysUserDepart>();
                    queryWrapper.eq("dep_id", depId).eq("user_id", userId);
                    SysUserDepart one = sysUserDepartService.getOne(queryWrapper);
                    if (one == null) {
                        sysUserDepartService.save(sysUserDepart);
                    }
                }
            }
            res101117 = new Res101117();
            res101117.setIsSuccess(true);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("101117", JSON.toJSONString(req), "给指定部门添加用户", log.buildErrmsg(e),
                LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "addSysUserDepart {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("101117", JSON.toJSONString(req), "给指定部门添加用户", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res101117);
        } else {
            log.addOperateLog("101117", JSON.toJSONString(req), "给指定部门添加用户", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101118", method = RequestMethod.POST)
    @ApiOperation(value = "删除指定部门的用户关系", notes = "删除指定部门的用户关系")
    @ResponseBody
    public Result<Res101118> deleteUserInDepart(@RequestBody GatewayRequest<Req101118> req) {
        log.debug(req, "deleteUserInDepart {}  ", "end");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101118 res101118 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<SysUserDepart>();
            queryWrapper.eq("dep_id", req.getContent().getDepId()).eq("user_id", req.getContent().getUserId());
            sysUserDepartService.remove(queryWrapper);
            res101118 = new Res101118();
            res101118.setIsSuccess(true);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("101118", JSON.toJSONString(req), "删除指定部门的用户关系", log.buildErrmsg(e),
                LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "deleteUserInDepart {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("101118", JSON.toJSONString(req), "删除指定部门的用户关系", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res101118);
        } else {
            log.addOperateLog("101118", JSON.toJSONString(req), "删除指定部门的用户关系", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101119", method = RequestMethod.POST)
    @ApiOperation(value = "批量删除指定部门的用户关系", notes = "批量删除指定部门的用户关系")
    @ResponseBody
    public Result<Res101119> batchDeleteUserInDepart(@RequestBody GatewayRequest<Req101119> req) {
        log.debug(req, "batchDeleteUserInDepart {}  ", "end");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101119 res101119 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<SysUserDepart>();
            queryWrapper.eq("dep_id", req.getContent().getDepId()).in("user_id",
                Arrays.asList(req.getContent().getUserIds().split(",")));
            sysUserDepartService.remove(queryWrapper);
            res101119 = new Res101119();
            res101119.setIsSuccess(true);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("101119", JSON.toJSONString(req), "批量删除指定部门的用户关系", log.buildErrmsg(e),
                LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "batchDeleteUserInDepart {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("101119", JSON.toJSONString(req), "批量删除指定部门的用户关系", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res101119);
        } else {
            log.addOperateLog("101119", JSON.toJSONString(req), "批量删除指定部门的用户关系", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101120", method = RequestMethod.POST)
    @ApiOperation(value = "校验手机号码是否唯一", notes = "校验手机号码是否唯一")
    @ResponseBody
    public Result<Res101120> checkOnlyPhone(@RequestBody GatewayRequest<Req101120> req) {
        log.debug(req, "checkOnlyPhone {}  ", "end");
        BizCodeEnum bizCodeEnum = BizCode.SUCCESS;
        Res101120 res101120 = new Res101120();
        res101120.setIsSuccess(true);
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            String id = req.getContent().getId();
            SysUser user = null;
            if (StringUtils.isNotEmpty(id)) {
                user = sysUserService.getById(id);
            }
            SysUser newUser = sysUserService
                .getOne(new QueryWrapper<SysUser>().lambda().eq(SysUser::getPhone, req.getContent().getPhone()));
            if (newUser != null) {
                if (user == null) {
                    // user为空=>新增模式=>只要phone存在则返回false
                    res101120.setIsSuccess(false);
                    res101120.setMessage("手机号码已存在");
                } else if (!id.equals(newUser.getId())) {
                    // 否则=>编辑模式=>判断两者ID是否一致
                    res101120.setIsSuccess(false);
                    res101120.setMessage("手机号码已存在");
                }
            }
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "checkOnlyPhone {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101120);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101121", method = RequestMethod.POST)
    @ApiOperation(value = "获取员工用户分页列表", notes = "获取员工用户分页列表")
    @ResponseBody
    public Result<Res101121> getStaffUserPage(@RequestBody GatewayRequest<Req101121> req) {
        log.debug(req, "getStaffUserPage {}  ", "begin");
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101121 res101121 = null;
        try {
            String userId = ThreadLocalScope.getAuthData().getUser();
            IPage<Map<String, Object>> pageList = sysUserService.getStaffUserPage(req.getContent(), userId);
            res101121 = new Res101121();
            res101121.setPage(pageList);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getStaffUserPage {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101121);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101122", method = RequestMethod.POST)
    @ApiOperation(value = "根据客户用户id查询客户员工用户列表", notes = "根据客户用户id查询客户员工用户列表")
    @ResponseBody
    public Result<Res101122> queryCustomerStaffUserListById(@RequestBody GatewayRequest<Req101122> req) {
        log.debug(req, "queryCustomerStaffUserListById {}  ", "end");
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101122 res101122 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            QueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>();
            queryWrapper.eq("parent_id", req.getContent().getId());
            queryWrapper.eq("del_flag", CommonConstant.DELETE_NO);
            queryWrapper.orderByDesc("create_time");
            List<SysUser> sysUserList = sysUserService.list(queryWrapper);
            res101122 = new Res101122();
            List<SysUserRes> list = CopyListUtils.copyList(sysUserList, SysUserRes.class);
            res101122.setList(list);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "queryCustomerStaffUserListById {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101122);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101123", method = RequestMethod.POST)
    @ApiOperation(value = "修改个人密码", notes = "修改个人密码")
    @ResponseBody
    public Result<Res101123> updatePersonalPassword(@RequestBody GatewayRequest<Req101123> req) {
        log.debug(req, "updatePersonalPassword {}  ", "end");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101123 res101123 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            String id = ThreadLocalScope.getAuthData().getUser();
            String oldPassword = "";
            try {
                oldPassword = RSAUtil.decryptByPrivateKey(req.getContent().getOldPassword(), privateKey);
            } catch (Exception e) {
                log.error(req, "旧密码私钥解密失败", e);
                throw new BusinessException("旧密码私钥解密失败");
            }
            String newPassword = "";
            try {
                newPassword = RSAUtil.decryptByPrivateKey(req.getContent().getNewPassword(), privateKey);
            } catch (Exception e) {
                log.error(req, "新密码私钥解密失败", e);
                throw new BusinessException("新密码私钥解密失败");
            }
            SysUser sysUser = sysUserService.getById(id);
            if (sysUser != null) {
                String analysisPwd = PasswordUtil.encrypt(sysUser.getUsername(), oldPassword, sysUser.getSalt());
                if (sysUser.getPassword().equals(analysisPwd)) {
                    String salt = GenerateCodeUtil.randomGen(8);
                    sysUser.setSalt(salt);
                    String passwordEncode = PasswordUtil.encrypt(sysUser.getUsername(), newPassword, salt);
                    sysUser.setPassword(passwordEncode);
                    sysUserService.updateById(sysUser);
                    bizCodeEnum = BizCode.SUCCESS;
                    res101123 = new Res101123();
                    res101123.setIsSuccess(true);
                } else {
                    bizCodeEnum = BizCode.OLD_PASSWORD_ERROR;
                }
            } else {
                bizCodeEnum = BizCode.NOT_USER;
            }
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("101123", JSON.toJSONString(req), "修改个人密码", log.buildErrmsg(e),
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "updatePersonalPassword {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("101123", JSON.toJSONString(req), "修改个人密码", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res101123);
        } else {
            log.addOperateLog("101123", JSON.toJSONString(req), "修改个人密码", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101124", method = RequestMethod.POST)
    @ApiOperation(value = "获取个人信息", notes = "获取个人信息")
    @ResponseBody
    public Result<Res101124> getPersonalInfo(@RequestBody GatewayRequest<Req101124> req) {
        log.debug(req, "getPersonalInfo {}  ", "begin");
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101124 res101124 = null;
        try {
            String id = ThreadLocalScope.getAuthData().getUser();
            SysUser sysUser = sysUserService.getById(id);
            if (sysUser != null) {
                res101124 = new Res101124();
                BeanUtils.copyProperties(sysUser, res101124);

                if(StringUtils.isNotBlank(res101124.getAvatar())){
                    String url= OssUtils.getUrl(res101124.getAvatar(),1);
                    res101124.setAvatarUrl(url);
                }

                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(id);
                QueryWrapper<SysUserRole> sysUserRoleQueryWrapper = new QueryWrapper<SysUserRole>();
                sysUserRoleQueryWrapper.setEntity(sysUserRole);
                List<SysUserRole> sysUserRoleList = sysUserRoleService.list(sysUserRoleQueryWrapper);
                if (!CollectionUtils.isEmpty(sysUserRoleList)) {
                    List<String> roleIds = new ArrayList<String>();
                    sysUserRoleList.forEach((n -> roleIds.add(n.getRoleId())));
                    res101124.setRoleIds(roleIds);
                    List<SysRole> sysRoleList = sysRoleService.listByIds(roleIds);
                    if (!CollectionUtils.isEmpty(sysRoleList)) {
                        List<String> roleNames = new ArrayList<String>();
                        sysRoleList.forEach((n -> roleNames.add(n.getRoleName())));
                        res101124.setRoleNames(roleNames);
                    }
                }
                SysUserDepart sysUserDepart = new SysUserDepart();
                sysUserDepart.setUserId(id);
                QueryWrapper<SysUserDepart> sysUserDepartQueryWrapper = new QueryWrapper<SysUserDepart>();
                sysUserDepartQueryWrapper.setEntity(sysUserDepart);
                List<SysUserDepart> sysUserDepartList = sysUserDepartService.list(sysUserDepartQueryWrapper);
                if (!CollectionUtils.isEmpty(sysUserDepartList)) {
                    List<String> departIds = new ArrayList<String>();
                    sysUserDepartList.forEach((n -> departIds.add(n.getDepId())));
                    res101124.setDepartIds(departIds);
                    List<SysDepart> sysDepartList = sysDepartService.listByIds(departIds);
                    if (!CollectionUtils.isEmpty(sysDepartList)) {
                        List<String> departNames = new ArrayList<String>();
                        sysDepartList.forEach((n -> departNames.add(n.getDepartName())));
                        res101124.setDepartNames(departNames);
                    }
                }
                bizCodeEnum = BizCode.SUCCESS;
            } else {
                bizCodeEnum = BizCode.NOT_USER;
            }
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getPersonalInfo {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101124);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101125", method = RequestMethod.POST)
    @ApiOperation(value = "修改个人信息", notes = "修改个人信息")
    @ResponseBody
    public Result<Res101125> updatePersonalInfo(@RequestBody GatewayRequest<Req101125> req) {
        log.debug(req, "updatePersonalInfo {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101125 res101125 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            String id = req.getContent().getId();
            if (StringUtil.isEmpty(id)) {
                id = ThreadLocalScope.getAuthData().getUser();
            }
            SysUser sysUser = sysUserService.getById(id);
            if (sysUser != null) {
                BeanUtils.copyProperties(req.getContent(), sysUser);
                sysUserService.updateById(sysUser);
                res101125 = new Res101125();
                res101125.setIsSuccess(true);
                bizCodeEnum = BizCode.SUCCESS;
            } else {
                bizCodeEnum = BizCode.NOT_USER;
            }
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("101125", JSON.toJSONString(req), "修改个人信息", log.buildErrmsg(e),
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "updatePersonalInfo {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("101125", JSON.toJSONString(req), "修改个人信息", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res101125);
        } else {
            log.addOperateLog("101125", JSON.toJSONString(req), "修改个人信息", bizCodeEnum.getMessage(),
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101132", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "根据角色code查询用户信息", notes = "根据角色code查询用户信息")
    @ResponseBody
    public Result<Res101132> findUserListByRoleCode(@RequestBody GatewayRequest<Req101132> req) {
        log.debug(req, "findUserListByRoleCode {}  ", "begin");
        try {
            Req101132 content = req.getContent();
            ValidationUtils.validateEntityStr(content);
            List<SysUserRes> sysUserList = sysUserService.findAllUserByRoleCode(content.getRoleCode());
            Res101132 res = new Res101132();
            res.setList(sysUserList);
            log.debug(req, "findUserListByRoleCode {}  ", "end-------");
            return Result.success(req, res);
        } catch (Exception e) {
            log.error(req, BizErrorCode101.LIST_ERROR.getMessage(), e);
            return Result.error(req, BizErrorCode101.LIST_ERROR.getCode(), StringUtils.isBlank(e.getLocalizedMessage())
                ? BizErrorCode101.LIST_ERROR.getMessage() : e.getLocalizedMessage());
        }
    }

    /**
     *
     * @param req
     * @return
     */
    @RequestMapping(value = "/101133", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "根据用户ID查询RoleCode", notes = "根据用户ID查询RoleCode")
    @ResponseBody
    public Result<Res101133> findRoleCodeListByUserId(@RequestBody GatewayRequest<Req101133> req) {
        log.debug(req, "findRoleCodeListByUserId {}  ", "begin");
        try {
            Req101133 content = req.getContent();
            ValidationUtils.validateEntityStr(content);
            Res101133 res = new Res101133();
            String roleCodes = sysUserService.findRoleCodes(content.getUserId());
            res.setObj(roleCodes);
            log.debug(req, "findRoleCodeListByUserId {}  ", "end-------");
            return Result.success(req, res);
        } catch (Exception e) {
            log.error(req, BizErrorCode101.LIST_ERROR.getMessage(), e);
            return Result.error(req, BizErrorCode101.LIST_ERROR.getCode(), BizErrorCode101.LIST_ERROR.getMessage());
        }
    }

    /**
     *
     * @param req
     * @return
     */
    @RequestMapping(value = "/101134", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "判断是否是超级管理员", notes = "判断是否是超级管理员")
    @ResponseBody
    public Result<Res101134> judgeIsSuperAdministrator(@RequestBody GatewayRequest<Req101134> req) {
        log.debug(req, "judgeIsSuperAdministrator {}  ", "end");
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101134 res101134 = null;
        try {
            res101134 = new Res101134();
            res101134.setABoolean(sysUserService.judgeIsSuperAdministrator(ThreadLocalScope.getAuthData().getUser()));
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "judgeIsSuperAdministrator {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101134);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101135", method = RequestMethod.POST)
    @ApiOperation(value = "查询所有用户列表", notes = "查询所有用户列表")
    @ResponseBody
    public Result<Res101135> getAllSysUserInfoList(@RequestBody GatewayRequest<Req101135> req) {
        log.debug(req, "getAllSysUserInfoList {}  ", "end");
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101135 res101135 = null;
        try {
            List<SysUserInfo> list = sysUserService.getAllSysUserInfoList();
            bizCodeEnum = BizCode.SUCCESS;
            res101135 = new Res101135();
            res101135.setList(list);
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getAllSysUserInfoList {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101135);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }


}
